package service

import (
	"Todolist/pkg/ctl"
	"Todolist/pkg/utils"
	"Todolist/repository/db/dao"
	"Todolist/repository/db/model"
	"Todolist/types"
	"context"
	"sync"
	"time"
)

var TaskSrvIns *TaskSrv
var TaskSrvOnce sync.Once

type TaskSrv struct {
}

func GetTaskSrv() *TaskSrv {
	TaskSrvOnce.Do(func() {
		TaskSrvIns = &TaskSrv{}
	})
	return TaskSrvIns
}
func (s *TaskSrv) CreateTask(ctx context.Context, req *types.CreateTaskReq) (resp interface{}, err error) {
	u, err := ctl.GetUserInfo(ctx)
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}
	user, err := dao.NewUserDao(ctx).FindUserByUserId(u.Id)
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}
	task := &model.Task{

		Uid:       user.ID,
		Title:     req.Title,
		Content:   req.Title,
		Status:    0,
		StartTime: time.Now().Unix(),
	}
	err = dao.NewTaskDao(ctx).CreateTask(task)
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}
	return ctl.RespSuccess(), nil
}

func (s *TaskSrv) ListTask(ctx context.Context, req *types.ListTaskReq) (resp interface{}, err error) {
	u, err := ctl.GetUserInfo(ctx)
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}
	tasks, total, err := dao.NewTaskDao(ctx).ListTask(req.Start, req.Limit, u.Id)
	tRespList := make([]*types.ListTaskResp, 0)
	for _, v := range tasks {
		tRespList = append(tRespList, &types.ListTaskResp{
			Id:        v.ID,
			Title:     v.Title,
			Content:   v.Content,
			View:      0, //放在内存中 定时写入数据库
			Status:    v.Status,
			CreatedAt: v.CreatedAt.Unix(),
			StartTime: v.StartTime,
			EndTime:   v.EndTime,
		})
	}
	return ctl.RespList(tRespList, total), nil
}

func (s *TaskSrv) ShowTask(ctx context.Context, req *types.ShowTaskReq) (resp interface{}, err error) {
	u, err := ctl.GetUserInfo(ctx)
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}
	task, err := dao.NewTaskDao(ctx).FindTaskByIdAndUserId(req.Id, u.Id)
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}
	task.AddView()
	tResp := &types.ListTaskResp{
		Id:        task.ID,
		Title:     task.Title,
		Content:   task.Content,
		View:      0, //放在内存中 定时写入数据库
		Status:    task.Status,
		CreatedAt: task.CreatedAt.Unix(),
		StartTime: task.StartTime,
		EndTime:   task.EndTime,
	}

	return ctl.RespSuccessWithData(tResp), nil

}

func (s *TaskSrv) UpdateTask(ctx context.Context, req *types.UpdateTaskReq) (resp interface{}, err error) {
	u, err := ctl.GetUserInfo(ctx) //拿到user信息
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}
	err = dao.NewTaskDao(ctx).UpdateTask(u.Id, req)
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}

	return ctl.RespSuccess(), nil
}

func (s *TaskSrv) SearchTask(ctx context.Context, req *types.SearchTaskReq) (resp interface{}, err error) {
	u, err := ctl.GetUserInfo(ctx) //拿到user信息
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}
	tasks, err := dao.NewTaskDao(ctx).SearchTask(u.Id, req.Info)
	// 返回形式是model 所以要转
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}
	tRespList := make([]*types.ListTaskResp, 0)
	for _, v := range tasks {
		tRespList = append(tRespList, &types.ListTaskResp{
			Id:        v.ID,
			Title:     v.Title,
			Content:   v.Content,
			View:      0, //放在内存中 定时写入数据库
			Status:    v.Status,
			CreatedAt: v.CreatedAt.Unix(),
			StartTime: v.StartTime,
			EndTime:   v.EndTime,
		})
	}

	return ctl.RespSuccessWithData(tRespList), nil
}

func (s *TaskSrv) DeleteTask(ctx context.Context, req *types.DeleteTaskReq) (resp interface{}, err error) {
	u, err := ctl.GetUserInfo(ctx) //拿到user信息
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}
	err = dao.NewTaskDao(ctx).DeleteTaskById(req.Id, u.Id)
	if err != nil {
		utils.LogrusObj.Info(err)
		return
	}

	return ctl.RespSuccess(), nil
}
